<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <title>
        Tag: Java |
        
        Darklovy</title>
    <meta name="description" content>
    <meta name="keywords" content>
    <meta name="HandheldFriendly" content="True">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="shortcut icon" href="/">
    <link rel="alternate" href="/atom.xml" title="Darklovy">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta property="og:type" content="website">
<meta property="og:title" content="Darklovy">
<meta property="og:url" content="https://www.ttxxly.top/tags/Java/page/3/index.html">
<meta property="og:site_name" content="Darklovy">
<meta property="og:locale" content="en">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Darklovy">

    <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
    <link href="//cdn.bootcss.com/node-waves/0.7.5/waves.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/style.css">
    <script>
        function setLoadingBarProgress(num) {
            document.getElementById('loading-bar').style.width = num + "%";
        }
    </script>
</head>
</html>
<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script> 
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				Darklovy
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-Home' href='/'>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-categories' href='/categories'>
								分类
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-tags' href='/tags'>
								标签
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-algorithm' href='https://book.algorithm.ttxxly.top/'>
								算法
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-python' href='https://book.python.ttxxly.top/'>
								python
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>
		
		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-Home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/categories" class="nav-categories nav">
				分类
			</a>
		
			<a href="/tags" class="nav-tags nav">
				标签
			</a>
		
			<a href="https://book.algorithm.ttxxly.top/" class="nav-algorithm nav">
				算法
			</a>
		
			<a href="https://book.python.ttxxly.top/" class="nav-python nav">
				python
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <div class='l_main'>
        
  <script>
    window.subData= { title:'Tag : Java'}
  </script>

<section class="post-list">
	
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/21/Java-Object-类/">
        Java Object 类
      </a>
    </h2>
    
    <time>
      Jan 21, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>在 Java 中，所有的类都直接或间接继承了<code>java.lang.Object</code> 类。Object 类是比较特殊的类，它是所有类的父类，是 Java 类层中的最高层类。当创建一个类时，总是在继承，除非某个类已经指定要从其他类继承，否则它就是从 <code>java.lang.Object</code>类继承而来的。</p>
<h3 id="Object类-方法详解"><a href="#Object类-方法详解" class="headerlink" title="Object类 方法详解"></a>Object类 方法详解</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">clone():Object</span><br><span class="line">equals(Object):<span class="keyword">boolean</span></span><br><span class="line">finalize():<span class="keyword">void</span></span><br><span class="line">getClass():Class&lt;?&gt;</span><br><span class="line">hashCode():<span class="keyword">int</span></span><br><span class="line">notify():<span class="keyword">void</span></span><br><span class="line">notifyAll():<span class="keyword">void</span></span><br><span class="line">registerNatives():<span class="keyword">void</span></span><br><span class="line">toString():String</span><br><span class="line">wait():<span class="keyword">void</span></span><br><span class="line">wait(<span class="keyword">long</span>):<span class="keyword">void</span></span><br><span class="line">wait(<span class="keyword">long</span>, <span class="keyword">int</span>):<span class="keyword">void</span></span><br></pre></td></tr></table></figure>
<h4 id="getClass-方法"><a href="#getClass-方法" class="headerlink" title="getClass() 方法"></a>getClass() 方法</h4><p>getClass() 方法是 Object 类定义的方法，它会返回对象执行的 Class 实例，然后使用此实例调用 getName() 方法可以取得类的名称。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">getClass().getName();</span><br></pre></td></tr></table></figure></p>
<h4 id="toString-方法"><a href="#toString-方法" class="headerlink" title="toString() 方法"></a>toString() 方法</h4><p>toString() 方法的功能是将一个对象返回为字符串形式，它会返回一个 String 实例。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ObjectInstance</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"在"</span>+getClass().getName()+<span class="string">"类中重写 toString() 方法"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="keyword">new</span> ObjectInstance());<span class="comment">//打印本类对象</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="equals-方法"><a href="#equals-方法" class="headerlink" title="equals()方法"></a>equals()方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">V</span></span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">OverWriteEquals</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String s1 = <span class="string">"123"</span>;</span><br><span class="line">        String s2 = <span class="string">"123"</span>;</span><br><span class="line">        System.out.println(s1.equals(s2));</span><br><span class="line">        V v1 = <span class="keyword">new</span> V();</span><br><span class="line">        V v2 = <span class="keyword">new</span> V();</span><br><span class="line">        System.out.println(v1.equals(v2));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/16/Java-类的继承/">
        Java 类的继承
      </a>
    </h2>
    
    <time>
      Jan 16, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>继承在面向对象开发思想中是一个非常重要的概念，它使整个程序架构具有一定的弹性，在程序中复用一些已经定义完善的类不仅可以减少软件开发周期，也可以提高软件的可维护性和扩展性。</p>
<p>其基本思想是基于某个父类的扩展，制定出一个新的子类，子类可以继承父类原有的属性和方法，也可以增加原来父类所不具备的属性和方法，或者直接重写父类中的某些方法。</p>
<h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Test</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//SomeSentence</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doSomething</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//SomeSentence</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> Test <span class="title">dolt</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Test();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test2</span> <span class="keyword">extends</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">super</span>();</span><br><span class="line">        <span class="keyword">super</span>.doSomething();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doSomethingnew</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//SomeSentence</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doSomething</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//SomeSentence</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> Test2 <span class="title">dolt</span><span class="params">()</span>  </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Test2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h3><p>当重写父类方法时，修改方法的修饰权限只能从小的范围到大的范围改变，例如，父类中的 doSomething() 方法的修饰权限为 protected，继承后子类中的方法 doSomething() 的修饰权限只能修改为 public，不能修改为 private。</p>


    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/15/Java-大数字运算/">
        Java 大数字运算
      </a>
    </h2>
    
    <time>
      Jan 15, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>在 java中提供了大数字的操作类，即 Java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算，其中BigInteger 类是针对大整数的处理，而BigDecimal 类则是针对大小数的处理类。</p>
<h3 id="BigInteger"><a href="#BigInteger" class="headerlink" title="BigInteger"></a>BigInteger</h3><p>BigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。BigInteger 支持任意精度的整数。</p>
<p>在 BigInteger 类中封装了多种操作，除了基本的加、减、乘、除操作之外，还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。</p>
<p>使用 BigInteger 类，可以实例化一个 BigInteger 对象，彬自动调用相应的构造函数。BigInteger 类具有很多构造函数，但最直接的一种是参数以字符串形式代表要处理的数字。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">BigInteger</span><span class="params">(String val)</span></span>;</span><br><span class="line"></span><br><span class="line">val 是十进制数字符串</span><br><span class="line"></span><br><span class="line">BigInteger twoInstance = <span class="keyword">new</span> BigInteger(<span class="string">"2"</span>);</span><br></pre></td></tr></table></figure>
<p>下面列举了 BigInteger 类中常用的几种运算方法<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">add</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做加法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">subftract</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做减法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">multiply</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做乘法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">divide</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做除法运算</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">remainder</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//做取余运算</span></span><br><span class="line"><span class="keyword">public</span> BigInteger[] divideAndReainder(BigInteger val);<span class="comment">//用数组返回余数和商，结果数组中第一个值为商，第二个值为余数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">pow</span><span class="params">(<span class="keyword">int</span> exponent)</span></span>;<span class="comment">//取参数的 exponent 次方操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">negate</span><span class="params">()</span></span>;<span class="comment">//取相反数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">shiftLeft</span><span class="params">(<span class="keyword">int</span> n)</span></span>;<span class="comment">//将数字左移 n 位，如果 n 为负数，做右移操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">shiftRight</span><span class="params">(<span class="keyword">int</span> n)</span></span>;<span class="comment">//将数字右移 n 位，如果 n 为负数，做左移操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">and</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//与操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">or</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//或操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//数字比较操作</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object x)</span></span>;<span class="comment">//当参数x是BigInteger类型的珠子并且数值相等时，返回true</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">min</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//返回较小的数字</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> BigInteger <span class="title">max</span><span class="params">(BigInteger val)</span></span>;<span class="comment">//返回较大的数字</span></span><br></pre></td></tr></table></figure></p>
<p><strong>例子：实现大数的各种运算操作</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BigIntegerDemo</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        BigInteger bigInstance = <span class="keyword">new</span> BigInteger(<span class="string">"4"</span>);、、实例化一个大数字</span><br><span class="line">        <span class="comment">//取该大数字 加 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"加法操作："</span>+bigInstance.add(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 减 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"减法操作："</span>+bigInstance.subtract(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 乘 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"乘法操作："</span>+bigInstance.multiply(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 除 2 的操作</span></span><br><span class="line">        System.out.println(<span class="string">"除法操作："</span>+bigInstance.divide(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>)));</span><br><span class="line">         <span class="comment">//取该大数字 除以 3 的商</span></span><br><span class="line">        System.out.println(<span class="string">"取商："</span>+bigInstance.divideAndReainder(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>))[<span class="number">0</span>]);</span><br><span class="line">          <span class="comment">//取该大数字 除以 3 的余数</span></span><br><span class="line">        System.out.println(<span class="string">"取商："</span>+bigInstance.divideAndReainder(<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>))[<span class="number">1</span>]);</span><br><span class="line">         <span class="comment">//取该大数字的 2 次方</span></span><br><span class="line">        System.out.println(<span class="string">"做 2 次方的操作："</span>+bigInstance.pow(<span class="number">2</span>));</span><br><span class="line">         <span class="comment">//取该大数字的相反数</span></span><br><span class="line">        System.out.println(<span class="string">"取相反数操作："</span>+bigInstance.negate());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="BigDecimal"><a href="#BigDecimal" class="headerlink" title="BigDecimal"></a>BigDecimal</h3><p>一般的 float 型和 double 型数据只可以用来做科学计算或工程计算，但由于在商业计算中要求数字精度比较高，所以要用到 java.math.BigDecimal 类。BigDecimal 类支持任何精度的定点数，可以用它来精确计算货币值。</p>
<h4 id="两个常用的构造方法"><a href="#两个常用的构造方法" class="headerlink" title="两个常用的构造方法"></a>两个常用的构造方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">publilc <span class="title">BigDecimal</span><span class="params">(<span class="keyword">double</span> val)</span></span>;<span class="comment">//将双精度型转换为 BigDecimal 类型</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">BigDecimal</span><span class="params">(String val)</span></span>;<span class="comment">//将字符串转换为 BigDecimal 类型</span></span><br></pre></td></tr></table></figure>
<p><strong>例子：高精度大小运算操作</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.math.BigDecimal;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BigDecimalDemo</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> location = <span class="number">10</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">        加法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> b1.add(b2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">        减法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> b1.subtract(b2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">        乘法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> b1.multiply(b2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">        除法运算</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> div(value1, value2, location);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//定义除法方法，参数分别为除数与被除数以及商小数点后的余数</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">div</span><span class="params">(<span class="keyword">double</span> value1, <span class="keyword">double</span> value2, <span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (b &lt;  <span class="number">0</span>) &#123;</span><br><span class="line">            System.out.printn(<span class="string">"b 值必须大于等于 0 "</span>);</span><br><span class="line">        &#125;</span><br><span class="line">         BigDecimal b1 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value1));</span><br><span class="line">        BigDecimal b2 = <span class="keyword">new</span> BigDecimal(<span class="keyword">double</span>.toString(value2));</span><br><span class="line">        <span class="keyword">return</span> b1.divide(b2, b, BigDecimal.ROUND_HALF_UP);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="面试题"><a href="#面试题" class="headerlink" title="面试题"></a>面试题</h3><h4 id="下列程序的输出结果是什么？"><a href="#下列程序的输出结果是什么？" class="headerlink" title="下列程序的输出结果是什么？"></a>下列程序的输出结果是什么？</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"><span class="keyword">import</span> java.lang.＊;</span><br><span class="line"><span class="keyword">public</span>   <span class="class"><span class="keyword">class</span>   <span class="title">Test</span>   </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span>   <span class="keyword">static</span>   <span class="keyword">void</span>   <span class="title">main</span><span class="params">(String[]   args)</span>  <span class="keyword">throws</span> NumberFormatException </span>&#123;</span><br><span class="line">      BigInteger   one=<span class="keyword">new</span>     BigInteger(<span class="string">"1"</span>);</span><br><span class="line">      BigInteger   two=<span class="keyword">new</span>     BigInteger(<span class="string">"2"</span>);</span><br><span class="line">      BigInteger   three=<span class="keyword">new</span>   BigInteger(<span class="string">"3"</span>);</span><br><span class="line">      BigInteger   sum=<span class="keyword">new</span>     BigInteger(<span class="string">"0"</span>);</span><br><span class="line">      sum.add(one);</span><br><span class="line">      sum.add(two);</span><br><span class="line">      sum.add(three);</span><br><span class="line">      System.out.println(sum.toString());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>A. 6</li>
<li>B. 0</li>
<li>C. 1 </li>
<li>D. 3</li>
</ul>
<p><strong>解析：</strong> 本题考的是Java中的大数类。<br>（1）BigInteger属于java.math.BigInteger，因此，在每次使用前都要import（输入）这个类，否则会提示找不到提示符。<br>（2）BigInteger构造方法有很多，如：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">BigInteger(String val)</span><br><span class="line"><span class="comment">//将BigInteger的十进制字符串表示形式转换为BigInteger</span></span><br><span class="line">BigInteger(String val，<span class="keyword">int</span> radix)</span><br><span class="line"><span class="comment">//将指定基数的BigInteger的字符串表示形式转换为BigInteger</span></span><br><span class="line">如要将<span class="keyword">int</span>型的<span class="number">2</span>转换为BigInteger型，要写为：</span><br><span class="line">BigInteger two=<span class="keyword">new</span> BigInteger(<span class="string">"2"</span>); <span class="comment">//注意2的双引号不能省略</span></span><br></pre></td></tr></table></figure></p>
<p>（3）BigInteger类模拟了所有的int型数学操作，如add()==+，divide()==-等，但注意进行数学运算时，不能直接使用数学运算符进行运算，必须使用其内部方法，而且其操作数也必须为BigInteger型。<br>如：two.add(2)就是一种错误的操作，因为2没有改为BigInteger型。<br>对本题而言，sum返回一个biginteger的值并不改变原来的变量，所以sum的值始终为0。如果想得到6的结果，必须显式地返回，代码如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">import</span> java.math.BigInteger;</span><br><span class="line"><span class="keyword">import</span> java.lang.＊;</span><br><span class="line"><span class="keyword">public</span>   <span class="class"><span class="keyword">class</span>   <span class="title">Test</span>   </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span>   <span class="keyword">static</span>   <span class="keyword">void</span>   <span class="title">main</span><span class="params">(String[]   args)</span>  <span class="keyword">throws</span> NumberFormatException </span>&#123;</span><br><span class="line">      BigInteger   one=<span class="keyword">new</span>     BigInteger(<span class="string">"1"</span>);</span><br><span class="line">      BigInteger   two=<span class="keyword">new</span>     BigInteger(<span class="string">"2"</span>);</span><br><span class="line">      BigInteger   three=<span class="keyword">new</span>   BigInteger(<span class="string">"3"</span>);</span><br><span class="line">      BigInteger   sum=<span class="keyword">new</span>     BigInteger(<span class="string">"0"</span>);</span><br><span class="line">      sum = sum.add(one);</span><br><span class="line">      sum = sum.add(two);</span><br><span class="line">      sum = sum.add(three);</span><br><span class="line">      System.out.println(sum.toString());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>答案：</strong> B</p>


    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/14/Java-随机数/">
        Java 随机数
      </a>
    </h2>
    
    <time>
      Jan 14, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>在实际开发中产生随机数的使用是很普遍的，所以在程序中进行随机数操作也是很重要的。</p>
<p>在 Java 中主要提供了两种方式产生随机数，分别是 Math 类的 random() 方法和 Random 类提供的产生各种数据类型随机数的方法。</p>
<h3 id="Math-random-方法"><a href="#Math-random-方法" class="headerlink" title="Math.random() 方法"></a>Math.random() 方法</h3><p>在 Math 类中存在一个 random() 方法，用于产生随机数字，这个方法默认生成大于等于 0.0 且小于 1.0 的 double 型随机数, 即 0 &lt;= Math.random() &lt; 1.0.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MathRandom</span></span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">GetAnyNum</span><span class="params">(<span class="keyword">double</span> num1, <span class="keyword">double</span> num2)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//产生 num1 ~ num2 之间的随机数</span></span><br><span class="line">        <span class="keyword">int</span> s = (<span class="keyword">int</span>)num1 + (<span class="keyword">int</span>)(Math.random() *(num2-num1));</span><br><span class="line">        <span class="keyword">if</span>(s % <span class="number">2</span> == <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> s;</span><br><span class="line">        &#125;<span class="keyword">else</span></span><br><span class="line">        <span class="keyword">return</span> s + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//调用产生随机数方法</span></span><br><span class="line">        System.out.println(<span class="string">"任意一个 2-32 之间的偶数："</span>+ getAnyNum(<span class="number">2</span>, <span class="number">32</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Random-类"><a href="#Random-类" class="headerlink" title="Random 类"></a>Random 类</h3><p>除了 Math 类的 random() 方法可以获取谁随机数之外，Java 还提供了一种获取随机数的方式，那就是 Java.util.Random 类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RandomDemo</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Random r =  <span class="keyword">new</span> Random();<span class="comment">//实例化一个 Random 类</span></span><br><span class="line">        <span class="comment">//随机产生一个整数</span></span><br><span class="line">        System.out.println(<span class="string">"随机产生一个整数："</span>+r.nextInt());</span><br><span class="line">        <span class="comment">//随机产生一个大于等于0且小于10的整数</span></span><br><span class="line">        System.out.println(<span class="string">"随机产生一个大于等于0且小于10的整数："</span>+r.nextInt(<span class="number">10</span>));</span><br><span class="line">        <span class="comment">//随机产生一个布尔型的值</span></span><br><span class="line">        System.out.println(<span class="string">"随机产生一个布尔型的值："</span>+r.nextBoolean());</span><br><span class="line">        <span class="comment">//随机产生一个双精度的值</span></span><br><span class="line">        System.out.println(<span class="string">"随机产生一个双精度的值："</span>+r.nextDouble());</span><br><span class="line">        <span class="comment">//随机产生一个浮点型的值</span></span><br><span class="line">        System.out.println(<span class="string">"随机产生一个浮点型的值："</span>+r.nextFloat());</span><br><span class="line">        <span class="comment">//随机产生一个概率密度为高斯分布的双精度值</span></span><br><span class="line">        System.out.println(<span class="string">"随机产生一个概率密度为高斯分布的双精度值："</span>+r.nextGaussian());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/11/Java-数学运算/">
        Java 数学运算
      </a>
    </h2>
    
    <time>
      Jan 11, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>在 Java 语言中提供了一个执行数学基本运算的 Math 类，该类包括常用的数学运算方法，如三角函数方法、指数函数方法、对数函数方法、平方根函数方法等一些常用数学函数，初次之外还提供了一些常用的数学常量，如PI、E等。</p>
<h3 id="Math-类"><a href="#Math-类" class="headerlink" title="Math 类"></a>Math 类</h3><p>在 Math 类中提供了众多数学函数方法，主要包括三角函数方法、指数函数方法、取整函数方法、取最大值、最小值和平均值函数方法，这些方法都被定义成 static 形式，方便调用。</p>
<h3 id="常用数学运算方法"><a href="#常用数学运算方法" class="headerlink" title="常用数学运算方法"></a>常用数学运算方法</h3><p>在 Math 类中的常用数学运算方法较多，大致可以将其分为 4 大类别。分别为 三角函数方法、指数函数方法、取整函数方法以及取最大值、最小值好绝对值函数方法。</p>
<ol>
<li>三角函数方法<br>在 Math 类中包含的三角函数方法如下：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">sin</span><span class="params">(<span class="keyword">double</span> a)</span></span>; 返回角的三角正弦</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">cos</span><span class="params">(<span class="keyword">double</span> a)</span></span>; 返回角的三角余弦</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">tan</span><span class="params">(<span class="keyword">double</span> a)</span></span>; 返回角的三角正切</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">asin</span><span class="params">(<span class="keyword">double</span> a)</span></span>; 返回一个值的反正弦</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">acos</span><span class="params">(<span class="keyword">double</span> a)</span></span>; 返回一个值的反余弦</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">atan</span><span class="params">(<span class="keyword">double</span> a)</span></span>; 返回一个值的反正切</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">toRadians</span><span class="params">(<span class="keyword">double</span> angdeg)</span></span>; 将角度转换为弧度</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">toDegrees</span><span class="params">(<span class="keyword">double</span> angrad)</span></span>; 将弧度转换为角度</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p><strong>特别注意的是：角度和弧度转换通常是不精确地</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TrigonometricFunction</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//取 90 °的正弦</span></span><br><span class="line">        System.out.println(<span class="string">"90度的正弦值："</span>+Math.sin(Math.PI/<span class="number">2</span>))；</span><br><span class="line">        <span class="comment">//取 0 ° 的余弦</span></span><br><span class="line">        System.out.println(<span class="string">"0度的余弦值："</span>+Math.cos(<span class="number">0</span>));</span><br><span class="line">        <span class="comment">//取 60 ° 的 正切</span></span><br><span class="line">        System.out.println(<span class="string">"60 度的正切值:"</span> + Math.tan(Math.PI / <span class="number">3</span>));</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>指数函数方法</li>
</ol>
<p>Math 类中与指数相关的函数方法如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">exp</span><span class="params">(<span class="keyword">double</span> a)</span></span>; <span class="comment">//用于获取 e 的 a 次方</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">log</span><span class="params">(<span class="keyword">double</span> a)</span></span>; <span class="comment">//用于取自然对数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">log10</span><span class="params">(<span class="keyword">double</span> a )</span></span>; <span class="comment">//用于取底数为10的对数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">sqrt</span><span class="params">(<span class="keyword">double</span> a )</span></span>;<span class="comment">//用于取 a 的平方根，其中 a 不能为负值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">cbrt</span><span class="params">(<span class="keyword">double</span> a )</span></span>;   <span class="comment">//用于取 a 的立方根</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">pow</span><span class="params">(<span class="keyword">double</span> a, <span class="keyword">double</span> b)</span></span>;<span class="comment">//用于取  a 的 b 次方</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ExponentFunction</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"e 的平方值："</span> + Math.exp(<span class="number">2</span>));</span><br><span class="line">        <span class="comment">//取以 e 为底 2 的对数</span></span><br><span class="line">        System.out.println(<span class="string">"以e为底2的对数："</span>+ Math.log(<span class="number">2</span>));</span><br><span class="line">        <span class="comment">//取以10 为底 2 的对数</span></span><br><span class="line">        System.out.println(<span class="string">"以10为底 2 的对数值："</span>+Math.log10(<span class="number">2</span>));</span><br><span class="line">        System.out.println(<span class="string">"4 的平方根值："</span> + Math.sqrt(<span class="number">4</span>));</span><br><span class="line">        System.out.println(<span class="string">"8 的立方根值："</span>+Math.cbrt(<span class="number">8</span>));</span><br><span class="line">        System.out.println(<span class="string">"2 的 2 次方值："</span> + Math.pow(<span class="number">2</span>,<span class="number">2</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>取整函数方法<br>在具体的问题中，取整操作使用也很普遍。<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">ceil</span><span class="params">(<span class="keyword">double</span> a)</span></span>;<span class="comment">//返回大于等于参数的最小整数 </span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">floor</span><span class="params">(<span class="keyword">double</span> a)</span></span>;   <span class="comment">//返回小于等于参数的最大整数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">rint</span><span class="params">(<span class="keyword">double</span> a)</span></span>;<span class="comment">//返回与参数最接近的整数，如果两个同为整数且童颜接近，则结果取整数、</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">round</span><span class="params">(<span class="keyword">float</span> a)</span></span>;<span class="comment">//将参数加上0.5后返回与参数最近的整数</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">round</span><span class="params">(<span class="keyword">double</span> a )</span></span>;<span class="comment">//将参数加上0.5之后返回与参数最近的整数，然后强制转换为长整型</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p><strong>由于数 1.0和 数 2.0 距离数 1.5 都是0.5个单位长度，因此返回偶数 2.0</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">intFunction</span></span>&#123;</span><br><span class="line">    <span class="comment">//返回第一个大于等于参数的整数</span></span><br><span class="line">    System.out.println(<span class="string">"使用 ceil() 方法取整："</span>+Math.ceil(<span class="number">5.2</span>));</span><br><span class="line">    <span class="comment">//返回第一个小于等于参数的整数</span></span><br><span class="line">    System.out.println(<span class="string">"使用 floor 方法取整："</span>+ Math.floor(<span class="number">2.5</span>));</span><br><span class="line">    <span class="comment">//返回与参数最接近的整数</span></span><br><span class="line">    System.out.println(<span class="string">"使用 rint() 方法取整："</span>+Math.rint(<span class="number">2.7</span>));</span><br><span class="line">    <span class="comment">//返回与参数最接近的整数</span></span><br><span class="line">    System.out.println(<span class="string">"使用 rint 方法取整："</span>+ Math.rint(<span class="number">2.5</span>));</span><br><span class="line">    <span class="comment">//将参数加上 0.5 之后返回最接近的整数</span></span><br><span class="line">    System.out.println(使用 round方法取整：<span class="string">"+Math.round(3.4f));</span></span><br><span class="line"><span class="string">    //将参数加上 0.5 之后返回最接近的整数，并将结果强制转换为长整型</span></span><br><span class="line"><span class="string">    System.out.println("</span>使用 round 方法取整：<span class="string">"+Math.round(2.5));</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure></p>
<ol start="4">
<li>取最大值、最小值、绝对值函数方法</li>
</ol>
<p>在程序中最常用的方法就是取最大值、最小值、绝对值等，在 Math 类中包括这些操作方法<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">max</span><span class="params">(<span class="keyword">double</span> a, <span class="keyword">double</span> b)</span></span>;<span class="comment">//取a与b之间的最大值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">min</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>;<span class="comment">//取a与b之间的最小值，参数是整型</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">min</span><span class="params">(<span class="keyword">long</span> a, <span class="keyword">long</span> b)</span></span>;<span class="comment">//取a与b之间的最小值，参数是长整型</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">float</span> <span class="title">min</span><span class="params">(<span class="keyword">float</span> a, <span class="keyword">float</span> b)</span></span>;<span class="comment">//取a与b之间的最小值，参数是浮点型</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">min</span><span class="params">(duble a, <span class="keyword">double</span> b)</span></span>;<span class="comment">//取a与b之间的最小值，参数是双精度型</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">abs</span><span class="params">(<span class="keyword">int</span> a)</span></span>;<span class="comment">//返回整形参数的绝对值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">abs</span><span class="params">(<span class="keyword">long</span> a)</span></span>;<span class="comment">//返回长整型参数的绝对值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">float</span> <span class="title">abs</span><span class="params">(<span class="keyword">float</span> a)</span></span>;<span class="comment">//返回浮点型参数的绝对值</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">abs</span><span class="params">(<span class="keyword">double</span> a)</span></span>;<span class="comment">//返回双精度型参数的绝对值</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AnyFunction</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"4 和 8 较大者："</span>+ Math.max(<span class="number">4</span>,<span class="number">8</span>));</span><br><span class="line">        <span class="comment">//取两个参数的最小值</span></span><br><span class="line">        System.out.println(<span class="string">"4.4 和 4 较小者："</span>+Math.min(<span class="number">4.4</span>, <span class="number">4</span>));</span><br><span class="line">        <span class="comment">//取参数的绝对值</span></span><br><span class="line">        System.out.println(<span class="string">"-7 的绝对值："</span>+ Math.abs(-<span class="number">7</span>));</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/10/Java-数字格式化/">
        Java 数字格式化
      </a>
    </h2>
    
    <time>
      Jan 10, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>数字的格式化在解决实际问题时使用非常普遍，如表示某超市的商品价格，不要八六两位有效数字。Java 主要对浮点型数据进行数字格式化操作，其中浮点型数据包括 double 和 float 型数据，在 java 中使用 <code>Java.text.DecimalFormat</code>格式化数字。</p>
<p>DecimalFormat 是 NumberFormat 的一个子类，用于格式化十进制数字。他可以将一些数字格式化为整数、浮点数、百分数等。通过使用该类可以为要输出的数字加上单位或控制数字的精度。</p>
<p>DecimalFormat 类中特殊字符说明</p>
<p><img src="/images/2019-1-10/1.png" alt></p>
<p><strong>格式化数字的例子</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.text.DecimalFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DecimalFormatSimpleDemo</span> </span>&#123;</span><br><span class="line">    <span class="comment">//使用实例化对象时设置格式化模式</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">SimpleFormat</span><span class="params">(String pattern, <span class="keyword">double</span> value)</span>   </span>&#123;</span><br><span class="line">        <span class="comment">//实例化 DecimalFormat 对象</span></span><br><span class="line">        DecimalFormat myFormat = <span class="keyword">new</span> DecimalFormat(pattern);</span><br><span class="line">        String output = myFormat.format(value);<span class="comment">//将数字格式化</span></span><br><span class="line">        System.out.println(value+<span class="string">" "</span>+pattern+<span class="string">" "</span>+output):</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//使用 applyPattern() 方法对数字进行格式化</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">UseApplyPatternMethodFormat</span><span class="params">(String pattern, <span class="keyword">double</span> value)</span> </span>&#123;</span><br><span class="line">        DecimalFormat myFormat = <span class="keyword">new</span> DecimalFormat();</span><br><span class="line">        myFormat.applyPattern(pattern);</span><br><span class="line">        System.out.println(value+<span class="string">" "</span>+pattern+<span class="string">" "</span>+myFormat.format(value));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SimpleFormat(<span class="string">"###,###.###"</span>, <span class="number">123456.789</span>);</span><br><span class="line">        SImpleFormat(<span class="string">"00000000.###kg"</span>, <span class="number">123456.789</span>);</span><br><span class="line">        <span class="comment">//按照格式模式格式化数字，不存在的位以 0 显示</span></span><br><span class="line">        SimpleFormat(<span class="string">"000000.000"</span>, <span class="number">123.78</span>);</span><br><span class="line">        <span class="comment">//调用静态 UseApplyPatternMethodDormat()方法</span></span><br><span class="line">        UseApplyPatternMethodFormat(<span class="string">"#.###%"</span>, <span class="number">0.789</span>);</span><br><span class="line">        <span class="comment">//将小数点够格式化为两位</span></span><br><span class="line">        UseApplyPatternMethodFormat(<span class="string">"###.##"</span>, <span class="number">123456.789</span>);</span><br><span class="line">        <span class="comment">//将数字格式为 千分数形式</span></span><br><span class="line">        UseApplyPatternMethodFormat(<span class="string">"0.00\u2030"</span>, <span class="number">0.789</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在 DecimalFormat 类中除了可以设置格式化模式来格式化数字之外，还可以使用一些特殊方法对数字进行格式化设置。<br>例如分组。。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Import java.text.DecimalFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DecimalMethod</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        DecimalFormat myFormat = <span class="keyword">new</span> DecimalFormat();</span><br><span class="line">        myFormat.setGroupingSize(<span class="number">2</span>);<span class="comment">//设置将数字每两个分一组</span></span><br><span class="line">        String output = myFormat.format(<span class="number">123456.789</span>);</span><br><span class="line">        System.out.println(<span class="string">"将数字以每两个数字分组"</span>+ output );</span><br><span class="line">        myFormat.setGroupingSize(<span class="keyword">false</span>);<span class="comment">//设置不允许分组</span></span><br><span class="line">        String output = myFormat.format(<span class="number">123456.789</span>);</span><br><span class="line">        System.out.println(<span class="string">"不允许数字分组"</span>+ output );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/09/java-包装类/">
        java 包装类
      </a>
    </h2>
    
    <time>
      Jan 9, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>Java 是一个面向对象的语言，但是 Java 中的基本数据类型却是不面向对象的，这在实际使用时存在很多的不便，为了解决这个不足，在设计类时为每个基本数据类型设计了一个对应的类进行代表，这样的八个和基本数据类型对应的类统称为包装类（wrapper class）。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">基本类型 大小 包装器类型</span><br><span class="line"><span class="keyword">boolean</span> /  Boolean</span><br><span class="line"><span class="keyword">char</span> <span class="number">16</span>bit Character </span><br><span class="line"><span class="keyword">byte</span> <span class="number">8</span>bit Byte </span><br><span class="line"><span class="keyword">short</span> <span class="number">16</span>bit Short </span><br><span class="line"><span class="keyword">int</span> <span class="number">32</span>bit Integer </span><br><span class="line"><span class="keyword">long</span> <span class="number">64</span>bit Long </span><br><span class="line"><span class="keyword">float</span> <span class="number">32</span>bit Float </span><br><span class="line"><span class="keyword">double</span> <span class="number">64</span>bit Double</span><br><span class="line"><span class="keyword">void</span> / Void</span><br></pre></td></tr></table></figure>
<p>Java中的包装器类有两个主要的目的：</p>
<ol>
<li>提供一种机制，将基本值“包装”到对象中，从而使基本值能够包含在为对象而保留的操作中，比如添加到Collections 中，或者从带对象返回值的方法中返回。注意，java5增加了自动装箱和拆箱，程序员过去需手工执行的许多包装操作，现在可以由java自动处理了。</li>
<li>为基本值提供分类功能。这些功能大多数于各种转换有关：在基本值和String对象间相互转换，在基本值和String对象之间按不同基数转换，如二进制、八进制和十六进制.</li>
</ol>
<h3 id="自动装箱和拆箱"><a href="#自动装箱和拆箱" class="headerlink" title="自动装箱和拆箱"></a>自动装箱和拆箱</h3><p>自动装箱和拆箱问题是Java中一个老生常谈的问题了，今天就来一些看一下装箱和拆箱中的若干问题。本文先讲述装箱和拆箱最基本的东西，再来看一下面试笔试中经常遇到的与装箱、拆箱相关的问题。</p>
<h4 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h4><p>在前面的文章中提到，Java为每种基本数据类型都提供了对应的包装器类型，至于为什么会为每种基本数据类型提供包装器类型在此不进行阐述，有兴趣的朋友可以查阅相关资料。在Java SE5之前，如果要生成一个数值为10的Integer对象，必须这样进行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Integer i = new Integer(100);</span><br></pre></td></tr></table></figure>
<p>而在从Java SE5开始就提供了自动装箱的特性，如果要生成一个数值为10的Integer对象，只需要这样就可以了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int i = 100;</span><br></pre></td></tr></table></figure>
<p>这个过程中会自动根据数值创建对应的 Integer对象，这就是装箱。<br>那什么是拆箱呢？顾名思义，跟装箱对应，就是自动将包装器类型转换为基本数据类型：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Integer i = 10; //装箱</span><br><span class="line">int index = i;  //拆箱</span><br></pre></td></tr></table></figure>
<p>简单一点说，装箱就是自动将基本数据类型转换为包装器类型；拆箱就是自动将包装器类型转换为基本数据类型。</p>
<p>注意：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Integer test = null;</span><br><span class="line">int f = test.intValue();</span><br></pre></td></tr></table></figure>
<p>上面的代码编写时不会报错，但编译是通不过的。</p>
<h4 id="实现机制"><a href="#实现机制" class="headerlink" title="实现机制"></a>实现机制</h4><p>我们就以Interger类为例，下面看一段代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String[] args)&#123;</span><br><span class="line">    Integer i = 10; //装箱</span><br><span class="line">    int index = i;  //拆箱</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>反编译class文件之后得到如下内容：</p>
<p>从反编译得到的字节码内容可以看出，在装箱的时候自动调用的是Integer的<code>valueOf(int)</code>方法。而在拆箱的时候自动调用的是Integer的<code>intValue</code>方法。</p>
<p>其他的也类似，比如Double、Character，不相信的朋友可以自己手动尝试一下。</p>
<p>因此可以用一句话总结装箱和拆箱的实现过程：</p>
<p><strong>装箱过程是通过调用包装器的valueOf方法实现的，而拆箱过程是通过调用包装器的xxxValue方法实现的。（xxx代表对应的基本数据类型）。</strong></p>
<hr>
<h4 id="面试问题"><a href="#面试问题" class="headerlink" title="面试问题"></a>面试问题</h4><p>虽然大多数人对装箱和拆箱的概念都清楚，但是在面试和笔试中遇到了与装箱和拆箱的问题却不一定会答得上来。下面列举一些常见的与装箱/拆箱有关的面试题，建议先自己尝试编译看源码做做。</p>
<ul>
<li>下面这段代码的输出结果是什么？</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Integer i1 = 100;</span><br><span class="line">        Integer i2 = 100;</span><br><span class="line">        Integer i3 = 200;</span><br><span class="line">        Integer i4 = 200;</span><br><span class="line">        </span><br><span class="line">        System.out.println(i1==i2);</span><br><span class="line">        System.out.println(i3==i4);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>也许有些朋友会说都会输出false，或者也有朋友会说都会输出true。这里注意“==”和“equal”的区别：</p>
<table>
<thead>
<tr>
<th>基本类型</th>
<th>==</th>
<th>equals</th>
</tr>
</thead>
<tbody>
<tr>
<td>字符串变量</td>
<td>对象在内存中的首地址</td>
<td>字符串内容</td>
</tr>
<tr>
<td>非字符串变量</td>
<td>对象在内存中的首地址</td>
<td>对象在内存中的首地址</td>
</tr>
<tr>
<td>基本类型</td>
<td>值</td>
<td>不可用</td>
</tr>
<tr>
<td>包装类</td>
<td>地址</td>
<td>内容</td>
</tr>
</tbody>
</table>
<p>结果：<br>true<br>false</p>
<p>为什么会出现这样的结果？输出结果表明 i1 和 i2 指向的是同一个对象，而 i3 和 i4 指向的是不同的对象。此时只需一看源码便知究竟，下面这段代码是Integer的<code>valueOf</code>方法的具体实现：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public static Integer valueOf(int i) &#123;</span><br><span class="line">        if(i &gt;= -128 &amp;&amp; i &lt;= IntegerCache.high)</span><br><span class="line">            return IntegerCache.cache[i + 128];</span><br><span class="line">        else</span><br><span class="line">            return new Integer(i);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>从这2段代码可以看出，在通过valueOf方法创建Integer对象的时候，如果数值在 [-128,127] 之间，便返回指向IntegerCache.cache中已经存在的对象的引用；否则创建一个新的Integer对象。</p>
<p>上面的代码中 i1 和 i2 的数值为100，因此会直接从cache中取已经存在的对象，所以 i1 和 i2 指向的是同一个对象，而 i3 和 i4 则是分别指向不同的对象。</p>
<ul>
<li>下面这段代码的输出结果是什么？</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Double i1 = 100.0;</span><br><span class="line">        Double i2 = 100.0;</span><br><span class="line">        Double i3 = 200.0;</span><br><span class="line">        Double i4 = 200.0;</span><br><span class="line">        </span><br><span class="line">        System.out.println(i1==i2);</span><br><span class="line">        System.out.println(i3==i4);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>也许有的朋友会认为跟上面一道题目的输出结果相同，但是事实上却不是。实际输出结果为：<br>false<br>false<br>至于具体为什么，读者可以去查看Double类的<code>valueOf</code>的实现。</p>
<p>在这里只解释一下为什么Double类的valueOf方法会采用与Integer类的valueOf方法不同的实现。很简单：在某个范围内的整型数值的个数是有限的，而浮点数却不是。</p>
<p>注意，Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的，Double、Float的valueOf方法的实现是类似的。</p>
<ul>
<li>下面这段代码的输出结果是什么？</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Boolean i1 = false;</span><br><span class="line">        Boolean i2 = false;</span><br><span class="line">        Boolean i3 = true;</span><br><span class="line">        Boolean i4 = true;</span><br><span class="line">         </span><br><span class="line">        System.out.println(i1==i2);</span><br><span class="line">        System.out.println(i3==i4);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出结果为：<br>true<br>true<br>至于为什么是这个结果，同样地，看了Boolean类的源码也会一目了然。下面是Boolean的valueOf方法的具体实现：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public static Boolean valueOf(boolean b) &#123;</span><br><span class="line">        return (b ? TRUE : FALSE);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>至于TRUE和FALSE的定义：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">     * The &#123;@code Boolean&#125; object corresponding to the primitive</span><br><span class="line">     * value &#123;@code true&#125;.</span><br><span class="line">     */</span><br><span class="line">    public static final Boolean TRUE = new Boolean(true);</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * The &#123;@code Boolean&#125; object corresponding to the primitive</span><br><span class="line">     * value &#123;@code false&#125;.</span><br><span class="line">     */</span><br><span class="line">    public static final Boolean FALSE = new Boolean(false);</span><br></pre></td></tr></table></figure>
<ul>
<li>谈谈Integer i = new Integer(xxx)和Integer i =xxx;这两种方式的区别。<br>当然，这个题目属于比较宽泛类型的。但是要点一定要答上，我总结一下主要有以下这两点区别：</li>
</ul>
<p>1）第一种方式不会触发自动装箱的过程；而第二种方式会触发；</p>
<p>2）在执行效率和资源占用上的区别。第二种方式的执行效率和资源占用在一般性情况下要优于第一种情况（注意这并不是绝对的）。</p>
<ul>
<li>下面这段代码的输出结果是什么？</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        Integer a = 1;</span><br><span class="line">        Integer b = 2;</span><br><span class="line">        Integer c = 3;</span><br><span class="line">        Integer d = 3;</span><br><span class="line">        Integer e = 321;</span><br><span class="line">        Integer f = 321;</span><br><span class="line">        Long g = 3L;</span><br><span class="line">        Long h = 2L;</span><br><span class="line">         </span><br><span class="line">        System.out.println(c==d);</span><br><span class="line">        System.out.println(e==f);</span><br><span class="line">        System.out.println(c==(a+b));</span><br><span class="line">        System.out.println(c.equals(a+b));</span><br><span class="line">        System.out.println(g==(a+b));</span><br><span class="line">        System.out.println(g.equals(a+b));</span><br><span class="line">        System.out.println(g.equals(a+h));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>先别看输出结果，读者自己想一下这段代码的输出结果是什么。这里面需要注意的是：当 “==” 运算符的两个操作数都是 包装器类型的引用，则是比较指向的是否是同一个对象，而如果其中有一个操作数是表达式（即包含算术运算）则比较的是数值（即会触发自动拆箱的过程）。另外，对于包装器类型，equals方法并不会进行类型转换。明白了这2点之后，上面的输出结果便一目了然：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">true</span><br><span class="line">false</span><br><span class="line">true</span><br><span class="line">true</span><br><span class="line">true</span><br><span class="line">false</span><br><span class="line">true</span><br></pre></td></tr></table></figure>
<p>第一个和第二个输出结果没有什么疑问。第三句由于 a+b 包含了算术运算，因此会触发自动拆箱过程（会调用intValue方法），因此它们比较的是数值是否相等。<br>而对于c.equals(a+b)会先触发自动拆箱过程，再触发自动装箱过程，也就是说a+b，会先各自调用intValue方法，得到了加法运算后的数值之后，便调用Integer.valueOf方法，再进行equals比较。<br>同理对于后面的也是这样，不过要注意倒数第二个和最后一个输出的结果（如果数值是int类型的，装箱过程调用的是Integer.valueOf；如果是long类型的，装箱调用的Long.valueOf方法）。</p>


    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/08/Java-对象的创建、引用、比较与销毁/">
        Java 对象的创建、引用、比较与销毁
      </a>
    </h2>
    
    <time>
      Jan 8, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>Java 是一门面向对象的程序设计语言，对象是由类抽象出来的，所有的问题都通过对象来处理，对象可以操作类的属性和方法解决相应的问题。，所以了解对象的产生、操作和消亡是十分必要的。</p>
<h3 id="对象的创建"><a href="#对象的创建" class="headerlink" title="对象的创建"></a>对象的创建</h3><p>对象可以认为是在一类事物中抽象某一个特例可以通过这个特例来处理这类事物出现的问题。在 Java 语言中通过 new 操作符来创建对象。 准确地说可以在 java 语言中使用 new 操作符调用构造方法创建对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Test test = <span class="keyword">new</span> Test();</span><br><span class="line">Test test = <span class="keyword">new</span> test(<span class="string">"a"</span>);</span><br></pre></td></tr></table></figure>
<h3 id="访问对象的属性和行为"><a href="#访问对象的属性和行为" class="headerlink" title="访问对象的属性和行为"></a>访问对象的属性和行为</h3><p>用户使用 new 操作符创建一个对象后，可以使用 “对象.类成员”来获取对象的属性和行为。对象的属性和行为在类中是通过类成员变量和成员方法的形式来表示的，所以当对象获取类成员时，也相应地获取了兑现的属性和行为。</p>
<p><strong>例子：展示对象是如何调用类成员的</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransferProperty</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i  = <span class="number">47</span>;    <span class="comment">//定义成员变量</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">class</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"调用 call 方法"</span>);</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;<span class="number">3</span>; i++)&#123;</span><br><span class="line">            System.out.println(i + <span class="string">""</span>);</span><br><span class="line">            <span class="keyword">if</span> ( i == <span class="number">2</span>)  &#123;</span><br><span class="line">                System.out.println(<span class="string">"\n"</span>);</span><br><span class="line">            )</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TransferProperty</span><span class="params">()</span> </span>&#123;&#125;<span class="comment">//定义构造方法</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        TransferProperty t1  = <span class="keyword">new</span> TransferProperty();  <span class="comment">//创建一个对象</span></span><br><span class="line">        TransferProperty t2 = <span class="keyword">new</span> TransferProperty();   <span class="comment">//创建另一个对象</span></span><br><span class="line">        t2.i = <span class="number">60</span>;  <span class="comment">//将类成员变量赋值为 60</span></span><br><span class="line">        <span class="comment">//使用第一个对象调用类成员变量</span></span><br><span class="line">        System.out.println(<span class="string">"第一个实例对象调用变量 i 的结果："</span>+ t1.i++);</span><br><span class="line">        t1.call();</span><br><span class="line">        <span class="comment">//使用第二个兑现调用类成员变量</span></span><br><span class="line">        System.out.println(<span class="string">"第二个实例对象调用变量 i 的结果："</span>+t2.i);</span><br><span class="line">        t2.call();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="对象的引用"><a href="#对象的引用" class="headerlink" title="对象的引用"></a>对象的引用</h3><p>在 Java 语言中尽管一切都可以看做对象，但真正操作标识符实质上是一个引用，那么引用在 Java 中应该如何体现？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">类名 对象引用名称</span><br></pre></td></tr></table></figure>
<p>如一个 Book 类的引用可以使用一下代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Book book;</span><br></pre></td></tr></table></figure></p>
<p>通常一个引用不一定需要有一个对象相关联。引用于对象相关联的语法如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Book book = <span class="keyword">new</span> Book();</span><br></pre></td></tr></table></figure></p>
<h3 id="对象的比较"><a href="#对象的比较" class="headerlink" title="对象的比较"></a>对象的比较</h3><p>在 Java 语言中有两种对象的比较方式，分别为“==”运算符和 equals() 方法。实质上这两种方式有着本质的区别。</p>
<p><strong>说明 == 和 equals 方法的区别</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Compare</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String c1 = <span class="keyword">new</span> String(<span class="string">"abc"</span>);</span><br><span class="line">        String c2 = <span class="keyword">new</span> String(<span class="string">"abc"</span>);</span><br><span class="line">        String c3 = c1 ;</span><br><span class="line">        <span class="comment">//使用“==”运算符比较 c2 和 c3</span></span><br><span class="line">        System,out.println(<span class="string">"c2==c3的运算结果为："</span>+(c2 == c3));</span><br><span class="line">        <span class="comment">//使用 </span></span><br><span class="line">            </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="对象的销毁"><a href="#对象的销毁" class="headerlink" title="对象的销毁"></a>对象的销毁</h3><p>每个对象都有生命周期，当对象的生命周期结束时，分配给该对象的内存地址将会被回收。在其他语言中需要手动回收废弃的对象，但是 Java 拥有一套完整的垃圾回收机制，用户不必担心废弃的对象占用内存，垃圾回收期将回收无用的但占用内存的资源。</p>
<p>在谈到垃圾回收机制之前，首先需要了解何种对象会被Java虚拟机视为垃圾。主要包括以下两种情况。</p>
<ul>
<li>对象引用超过其作用范围，这个对象将被视为垃圾。</li>
<li>将对象赋值为 null</li>
</ul>
<p>虽然垃圾回收机制已经很完善，但垃圾回收期只能回收那些由 new 操作符创建的对象。如果某些对象不是通过 new 操作符在内存中获取一块内存区域，这种对象可能不能被垃圾回收机制所识别，所以在 Java 中提供了一个 finalize() 方法。</p>
<p>这个方法是 Object 类的方法，它被声明为 protected，用户可以在自己的类中定义这个方法。如果用户在类中定义了 finalize() 方法，在垃圾回收时会搜狐先调用该方法，在下一次垃圾回收动作发生时，才能真正回收被对象占用的内存。</p>
<p><strong>有一点需要明确的是，垃圾回收 或 finalize() 方法不保证一定会发生，如 Java 虚拟机内存损耗殆尽时，它是不会执行垃圾回收的。</strong> </p>


    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/08/Java-类的主方法/">
        Java 类的主方法
      </a>
    </h2>
    
    <time>
      Jan 8, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>主方法是类的入口点，它定义了程序从何处开始；主方法提供对程序流向的控制，Java 编译器通过主方法来执行程序。主方法的语法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在主方法的定义中可以看到其具有以下特性：</p>
<ul>
<li>主方法是静态的，所以如要直接在主方法中调用其他方法，则该方法必须也是静态的。</li>
<li>主方法没有返回值</li>
<li>主方法的形参为数组。其中 args[0] ~ args[n]分别代表程序的第一个参数到第n个参数，可以使用 args.length 获取参数的个数。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestMain</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;args.length; i++) &#123;  <span class="comment">//根据参数个数做循环操作</span></span><br><span class="line">            System.out.println(args[i]);    <span class="comment">//循环打印参数内容</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
    <div class='post-wrapper'>
      <article class="post reveal">
  <section class="meta">
    
    <h2 class="title">
      <a href="/2019/01/01/Java-静态变量、常量与方法/">
        Java 静态变量、常量与方法
      </a>
    </h2>
    
    <time>
      Jan 1, 2019
    </time>
		
  </section>
  <section class="article typo">
	  <p>由 static 修饰的变量、常量和方法被称做静态变量、常量和方法。</p>
<p>有时，在处理问题时会需要两个类在同一个内存区域共享一个数据。例如，在球类中使用 PI 这个常量，可能出了本类需要这个常量之外，在另外一个圆类中也需要使用这个常量。这时没有必要在两个类中同时创建 PI 常量，因为这样系统会将这两个不在同一个类中定义的常量分配到不同的内存空间中。为了解决这个问题，可以将这个常量设置为静态的。</p>
<p>被声明为 static 的变量、常量和方法被称为静态成员。静态成员属于类所有，区别于个别对象，可以在本类或其他类使用类名和 “.” 运算符调用静态成员。</p>
<h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StaticTest</span></span>&#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> <span class="keyword">double</span> PI = <span class="number">3.1415</span>;    <span class="comment">//在类中定义静态常量</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> id;      <span class="comment">//在类中定义静态变量</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">method1</span><span class="params">()</span></span>&#123;   <span class="comment">//在类中定义静态方法</span></span><br><span class="line">        <span class="comment">//do something</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method2</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(StaticTest.PI);  <span class="comment">//调用静态常量</span></span><br><span class="line">        System.out.println(StaticTest.id);  <span class="comment">//调用静态变量</span></span><br><span class="line">        StaticTest.method1();   <span class="comment">//调用静态方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><p>在 Java 中规定不能讲方法体内的局部变量声明为 static 的。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">example</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span>    </span>&#123;</span><br><span class="line">        <span class="keyword">static</span> <span class="keyword">int</span> i = <span class="number">0</span>;   <span class="comment">//这句是错误的。</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="技巧"><a href="#技巧" class="headerlink" title="技巧"></a>技巧</h3><p>如果在执行类时，希望先执行类的初始化操作，可以使用 static 定义一个静态区域。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">example</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span>&#123;</span><br><span class="line">        <span class="comment">//some</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>


    
    
    
	  <div class="full-width auto-padding tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
  </section>
</article>
    </div>
  
</section>


  <nav id="page-nav">
    
    <a class="prev" rel="prev" href="/tags/Java/page/2/">
      <span class="icon icon-chevron-left"></span>
      <span class="text">Previous</span>
    </a>
    
    
    <a class="next" rel="next" href="/tags/Java/page/4/">
      <span class="text">Next</span>
      <span class="icon icon-chevron-right"></span>
    </a>
    
  </nav>
  

      </div>
      <aside class='l_side'>
        
  <section class='m_widget links'>
<div class='header'>Links</div>
<div class='content'>
    <ul class="entry">
    
        <li><a class="flat-box" target="_blank" href="https://iiong.com/">
            <div class='name'>淮城一只猫</div>
        </a></li>
    
    </ul>
</div>
</section>

  <section class='m_widget categories'>
<div class='header'>Categories</div>
<div class='content'>
    
    <ul class="entry">
    
        <li><a class="flat-box" href="/categories/Android基础/"><div class='name'>Android基础</div><div class='badget'>1</div></a></li>
    
    </ul>
    
</div>
</section>

  
<div class="m_widget tagcloud">
    <div class="header">Tags</div>
    <div class='content'>
        <a href="/tags/Android/" style="font-size: 14px; color: #808080">Android</a> <a href="/tags/HTML/" style="font-size: 14px; color: #808080">HTML</a> <a href="/tags/Java/" style="font-size: 20px; color: #000">Java</a> <a href="/tags/Python/" style="font-size: 14px; color: #808080">Python</a>
    </div>
</div>



      </aside>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/stkevintan" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://twitter.com/kevinsfork" class="social twitter"
          target="_blank" rel="external">
          <span class="icon icon-twitter"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a> designed by <a href="http://keyin.me/" target="_blank">Kevin Tan</a>.</div>
  
</footer>


  <script>setLoadingBarProgress(80);</script>
  

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
<script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
<script src="/js/jquery.fitvids.js"></script>
<script>
	var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
	var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
	var ALGOLIA_API_KEY = "";
	var ALGOLIA_APP_ID = "";
	var ALGOLIA_INDEX_NAME = "";
  var AZURE_SERVICE_NAME = "";
  var AZURE_INDEX_NAME = "";
  var AZURE_QUERY_KEY = "";
  var BAIDU_API_ID = "";
  var SEARCH_SERVICE = "hexo";
  var ROOT = "/"||"/";
  if(!ROOT.endsWith('/'))ROOT += '/';
</script>
<script src="/js/search.js"></script>
<script src="/js/app.js"></script>


  <script>setLoadingBarProgress(100);</script>
</body>
</html>
